package com.pignest.hotel.ui.reception;
import java.util.List;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;

import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;


import org.zkoss.zul.Window;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.beans.RoomType;


import com.pignest.hotel.definition.RoomLiveStatus;
import com.pignest.hotel.definition.RoomUseStatus;

import com.pignest.hotel.service.RoomService;
import com.pignest.hotel.spring.Services;


public class SelectRoomWindow extends Window implements EventListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1847388860936731093L;

	/**
	 * 选择空干净的房间
	 */



	private RoomService roomService;
	
	public Living living;
    public String roomTypeId;
	private Room selectRoom;
   
	public Listbox selectRoomListbox;
    public SelectRoomWindow() {
		roomService =Services.getRoomService();
		addEventListener("onCreate", this);
	}
	public String getRoomTypeId() {
		return roomTypeId;
	}
	public void setRoomTypeId(String roomTypeId) {
		this.roomTypeId = roomTypeId;
	}
	 public Room getSelectRoom() {
			return selectRoom;
		}
		public void setSelectRoom(Room selectRoom) {
			this.selectRoom = selectRoom;
		}
	private boolean success;
	
	
	@Override
	public void onEvent(Event event) throws Exception {
		// TODO Auto-generated method stub
		if ("onCreate".equals(event.getName())) {
			//nameTextbox = (Listbox) getFellow("nameField");
			selectRoomListbox=(Listbox) getFellow("selectRoomListbox");
			querySelectRoom();
		}
	}
	public void querySelectRoom(){
		RoomType roomType =roomService.getRoomType(this.roomTypeId);
		List <Room>l=roomService.listRooms(roomType);
		for(final Room r:l){
			Listitem listitem = new Listitem();
			listitem.setParent(selectRoomListbox);
			listitem.appendChild(new Listcell(r.getName()));//房间名
			listitem.appendChild(new Listcell(r.getType().getName()));//房型
			//RoomStatus
			listitem.appendChild(new Listcell(RoomLiveStatus.valueof(r.getLiveStatus()).getName()+RoomUseStatus.valueof(r.getUseStatus()).getName()));//状态
			listitem.appendChild(new Listcell(r.getFloor()));//楼层
			listitem.appendChild(new Listcell(r.getBuilding()));//分栋
			listitem.appendChild(new Listcell(r.getRepairStatus()));//维修状态
			 
			String canLive="是";
			if(roomService.canLiving(r)) 
				canLive="是";
			else
				canLive="否";
			
			listitem.appendChild(new Listcell(canLive));//是否可以入住
			listitem.addEventListener(Events.ON_CLICK,  new EventListener(){
				public void onEvent(Event event) throws Exception {   
					setSelectRoom(r);
				}
			});
		}
	}
	public Living getLiving() {
		return living;
	}
	public void setLiving(Living living) {
		this.living = living;
	}
    public void canclSelect(){
    	selectRoomListbox.clearSelection();
    	setSelectRoom(null);
    	this.detach();
    }
  
}
